﻿<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0,minimum-scale=1.0, user-scalable=no" />
<link rel="shortcut icon" href="/Images/ico/favicon.ico" type="image/x-icon">
<link rel="stylesheet" type="text/css" href="/css/swiper.min.css" />
<link rel="stylesheet" type="text/css" href="/css/style.css" />
<link rel="stylesheet" type="text/css" href="/css/isCategory.css">
<link type="text/css" rel="stylesheet" href="/layui/css/layui.css"  media="all">
<script type="text/javascript" src="/layui/layui.js" charset="utf-8"></script>
<script src="/js/jquery.min.js" type="text/javascript"></script>
<script src="/js/swiper.min.js" type="text/javascript"></script>
<script type="text/javascript" src="/js/custom.js"></script>
<script type="text/javascript" src="/js/layui-post.js"></script>
<style type="text/css">
.layui-layer-nobg{border-radius: 15px!important;background:#FFFFFF;}
.spaceon{background: #f7eec5!important;color: #ff6a00;border: 1px solid #ff9600!important;}
</style>
</head>
<body>
<div style="background: #ffffff;height: 100%;padding: 10px;border-radius: 0 0 15px 15px;margin-top: 0;" class="lists">
    <div id="choosespaces" style="margin-bottom: 80px;"></div>
    <div style="width: 100%; position: fixed;bottom: 0;height: 80px;z-index: 2;background: #f6f6f6;left: 0;">
        <div style="height: 30px;line-height: 30px;padding-left: 10px;border-bottom: 1px solid #eeeeee">已选规格：<span id="choosesname"></span></div>
        <div class="showaddcart" style="width: 100%;">
            <div style="width: 40%;float: left; text-align: left;margin-right: 30px;line-height: 50px;padding: 0px 0px 15px 0px;">
                <span style="font-weight: bold;padding-left: 10px;">总计<span></span><span style="color: red;font-weight: bold;">￥<span><span style="color: red;font-weight: bold;font-size: 20px;" id="count">0<span>
            </div>
            <div style="width: 40%;float: right; text-align: right;margin-right: 10px;line-height: 50px;padding: 0px 0px 15px 0px;">
                <img src="/img/cart_01.png" class="img-responsive join" alt="..." />
                <div class="num_con">
                    <img src="/img/num_l.png" class="img-responsive num_l" alt="..." />
                    <span class="number">0</span>
                    <img src="/img/num_r.png" class="img-responsive num_r" alt="..." />
                </div>
            </div>
        </div>
    </div>
</div>
</body>
<script src="/js/isCategory.js"></script>
<script>
$.ajaxSetup({contentType: "application/json; charset=utf-8"});
var userInfo = $.parseJSON(sessionStorage.getItem("userInfo"));
var goodsIds = parent.goodsIds,reqdataList = parent.reqdataList,spacesList = parent.spacesList,spacesNameList = parent.spacesNameList,goodsDiscount = 10;
layui.use(['layer','form'], function(){
    $("#choosespaces").html("")
    $.post("/cateringSpaces/selectSpaces",JSON.stringify({goodsId:getQueryString("goodsId")}),function (res){ //shop：加入购物车
        goodsDiscount = res.data[0].discount
        $.post("/cateringCart/select",JSON.stringify({userId:userInfo.userId,goodsId:getQueryString("goodsId")}),function (res){
            for(var i in res.data){
                if(!isEmpty(res.data[i].spacesId) && spacesList.length === 0) spacesList = res.data[i].spacesId.split(",")
            }
        })
        for(var i in res.data){
            $("#choosespaces").append(
                '<div>' +
                '<div style="width: 100%;height: 40px;line-height: 40px;margin-left: 1.5vw;">'+res.data[i].spacesName+'</div>' +
                '<div style="width: 100%;" id="'+res.data[i].sysId+'" class="'+getQueryString("goodsId")+i+'">' +
                '</div>' +
                '</div>'
            )
            for(var j in res.data[i].childList){
                var cla = "";
                if(j == 0 && spacesList.length == i) {
                    cla = j == 0 ? "spaceon" : ""
                    if(spacesList.indexOf(res.data[i].childList[j].sysId) > -1) {
                        spacesList.splice($.inArray(res.data[i].childList[j].sysId,spacesList),1)
                        spacesNameList.splice($.inArray(res.data[i].childList[j].spacesName,spacesname),1)
                    }
                    spacesList.push(res.data[i].childList[j].sysId)
                    spacesNameList.push(res.data[i].childList[j].spacesName)
                    $("#choosesname").html($.makeArray(spacesNameList).join(","))
                }
                $("#"+res.data[i].sysId).append(
                    '<button onclick="addSpace(\''+getQueryString("goodsId")+i+'\',\''+res.data[i].childList[j].sysId+'\')" id="'+res.data[i].childList[j].sysId+'" class="'+cla+'" style="padding: 5px 10px 5px 10px;border-radius: 5px;font-size: 12px;margin: 1.5vw;background: #fff;border: 1px solid #bfbdbd;" data-value="'+res.data[i].childList[j].price+'" data-name="'+res.data[i].childList[j].spacesName+'">'+res.data[i].childList[j].spacesName+'</button>'
                )
            }
        }
        //如果存在选择的商品，回显对应的规格
        for(var i in spacesList) $("#"+spacesList[i]).addClass("spaceon")
        if(spacesNameList.length > 0) $("#choosesname").html($.makeArray(spacesNameList).join(","))
        //查询已经选择的数量和规格并回显
        for(var i in reqdataList){
            if(reqdataList[i].goodsId === getQueryString("goodsId")){
                $(".join").hide();
                $(".num_con").show();
                $(".number").text(reqdataList[i].goodsNum);
                for(var j in spacesList){
                    $("#"+spacesList[i]).addClass("spaceon")
                }
            }
        }
        var summ = 0;
        $("#choosespaces button").each(function () {
            if($(this).attr("class") === "spaceon") {
                summ = summ + $(this).attr("data-value")/1
                if(spacesNameList.indexOf($(this).attr("data-name")) > -1){
                    spacesNameList.splice($.inArray($(this).attr("data-name"),spacesNameList),1)
                }
                spacesNameList.push($(this).attr("data-name"))
            }
        })
        $("#count").html((summ/1*res.data[0].discount/10).toFixed(2))
        $("#choosesname").html($.makeArray(spacesNameList).join(","))
        var objLi = $(".lists");
        //点击购物车
        $(objLi).find(".join").click(function () {
            $(".join").hide();
            $(".num_con").show();
            var goodsId = getQueryString("goodsId");
            var reqdata={};
            var number = 1;
            if(goodsIds.indexOf(goodsId) === -1) {
                goodsIds.push(goodsId)
                reqdata.goodsId = goodsId;
                reqdata.goodsNum = number
                reqdata.userId = userInfo.userId
                reqdata.goodsPrice = $("#count").html()
                reqdata.spacesId = $.makeArray(parent.spacesList).join(",")
                reqdataList.push(reqdata);
            }else {
                for(var i in reqdataList){
                    if(reqdataList[i].goodsId === goodsId) {
                        reqdataList[i].goodsNum = number;
                        reqdataList[i].goodsPrice = (summ/1*goodsDiscount/10).toFixed(2);
                    }
                }
            }
            $(objLi).find(".number").text(1);
            addCart();
        });
        //点击减数量
        $(objLi).find(".num_l").click(function () {
            var goodsId = getQueryString("goodsId");
            var number = parseInt($(objLi).find(".number").text()) - 1;
            if (number > 0) {
                $(objLi).find(".number").text(number);
                if(goodsIds.indexOf(goodsId) > -1){
                    for(var i in reqdataList){
                        if(reqdataList[i].goodsId === goodsId) reqdataList[i].goodsNum = number;
                    }
                }
            } else {
                if(goodsIds.indexOf(goodsId) > -1) {
                    goodsIds.splice($.inArray(goodsId,goodsIds),1)
                    reqdataList.splice($.inArray(goodsId,reqdataList),1)
                }
                $(".number").text(number);
                $(objLi).removeClass("on");
                $(".join").show();
                $(".num_con").hide();
            }
            addCart();
        });
        //点击加数量
        $(objLi).find(".num_r").click(function () {
            var goodsId = getQueryString("goodsId");
            var reqdata={};
            var number = parseInt($(objLi).find(".number").text()) + 1;
            if(goodsIds.indexOf(goodsId) === -1) {
                goodsIds.push(goodsId)
                reqdata.goodsId = goodsId;
                reqdata.goodsNum = number
                reqdata.goodsPrice = summ
                reqdata.userId = userInfo.userId
                reqdata.spacesId = $.makeArray(parent.spacesList).join(",")
                reqdataList.push(reqdata);
            }else {
                for(var i in reqdataList){
                    if(reqdataList[i].goodsId === goodsId) {
                        reqdataList[i].spacesId = $.makeArray(parent.spacesList).join(",")
                        reqdataList[i].goodsNum = number;
                        reqdataList[i].goodsPrice = (summ/1*goodsDiscount/10).toFixed(2);
                    }
                }
            }
            $(objLi).find(".number").text(number);
            addCart();
        });
    })
})
//规格调整
function addSpace(goodsId,spacesId){
    var summ = 0;spacesList = [];spacesNameList = [];
    $("."+goodsId+" button").each(function () {
        if($(this).attr("id") !== spacesId) {
            $(this).removeClass("spaceon")
            if($.inArray($(this).attr("id"),spacesList) > -1) spacesList.splice($.inArray($(this).attr("id"),spacesList),1)
        }
    })
    $("#"+spacesId).addClass("spaceon");
    $("#choosespaces button").each(function () {
        if($(this).attr("class") === "spaceon") {
            summ = summ + $(this).attr("data-value")/1
            spacesNameList.push($(this).attr("data-name"))
            spacesList.push($(this).attr("id"))
        }
    })
    $("#count").html((summ/1*goodsDiscount/10).toFixed(2))
    $("#choosesname").html($.makeArray(spacesNameList).join(","))
    for(var i in reqdataList){
        if(reqdataList[i].goodsId === getQueryString("goodsId")) {
            reqdataList[i].goodsPrice = (summ/1*goodsDiscount/10).toFixed(2);
        }
    }
    parent.spacesList = spacesList
    parent.spacesNameList = spacesNameList
    for(var i in reqdataList){
        if(reqdataList[i].goodsId === getQueryString("goodsId")) reqdataList[i].spacesId = $.makeArray(parent.spacesList).join(",")
    }
    addCart();
}
//父级购物车数量
function addCart(){
    parent.goodsIds = goodsIds
    parent.reqdataList = reqdataList
    parent.CountNumPriceLayer(getQueryString("goodsId"),$(".number").html(),$("#count").html()/1);
}
</script>
</html>
